package com.example.covid19.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.Dto.editdto;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.User;
import com.example.covid19.entity.UserRole;
import com.example.covid19.entity.Userinfo;
import com.example.covid19.mapper.UserinfoMapper;
import com.example.covid19.service.UserRoleService;
import com.example.covid19.service.UserService;
import com.example.covid19.service.UserinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author BaiZhengChun
 * @since 2022-10-19
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements UserinfoService {
    private final UserService userService;
    private final UserRoleService userRoleService;

    public UserinfoServiceImpl(UserService userService, UserRoleService userRoleService) {
        this.userService = userService;
        this.userRoleService = userRoleService;
    }

    @Override
    public Result edit(editdto editdto) {
        User user = new User();

        user.setUserid(Integer.parseInt(StpUtil.getLoginId().toString()));
//        user.setUsrid(editdto.getUsrid());
        int k = Integer.parseInt(editdto.getUsrid());
        int i = editdto.getStudentid();
        user.setStudentid(editdto.getStudentid());
        user.setPhone(editdto.getPhone());
        user.setUsername(editdto.getUsername());
        boolean flag = false;
        flag = userService.updateById(user);
        edrl(i,k);
        if (flag) {
            return Result.success(200, "更新成功", null);
        } else {
            return Result.success(200, "更新失败", null);
        }

    }
    public void edrl(int i,int k){
        User user = userService.getOne(new QueryWrapper<User>().eq(
                "studentid", Integer.valueOf(i)
        ));
        UserRole userRole = new UserRole();
        userRole.setRoleid(k);
        userRole.setUsrid(user.getUsrid());
        boolean flag = userRoleService.updateById(userRole);
//        System.out.println("1111111");
//        System.out.println(flag);
//        System.out.println("i = "+ i);
//        System.out.println("k = "+ k);
    }

}
